//package com.vgemv.jsutilitysdk.StreamToVgemvService;
//
//import android.os.Handler;
//import android.os.HandlerThread;
//import android.os.Message;
//
//
//import com.vgemv.jsutilitysdk.Request.JsRequestManager.JsRequestManager;
//import com.vgemv.jsutilitysdk.md5.GajMd5Service;
//import com.vgemv.jsutilitysdk.Request.JsRequestManager.JsRequestCallback;
//import com.vgemv.jsutilitysdk.Request.JsRequestInterface;
//import com.vgemv.jsutilitysdk.model.VgemvModel.EndLiveRequest;
//import com.vgemv.jsutilitysdk.model.VgemvModel.LiveRoomEntity;
//import com.vgemv.jsutilitysdk.model.VgemvModel.LiveRoomGetRequest;
//import com.vgemv.jsutilitysdk.model.VgemvModel.LiveRoomListRequest;
//import com.vgemv.jsutilitysdk.model.VgemvModel.LiveRoomListResponse;
//import com.vgemv.jsutilitysdk.model.VgemvModel.LoginRequestEntity;
//import com.vgemv.jsutilitysdk.model.VgemvModel.LoginResponse;
//import com.vgemv.jsutilitysdk.model.VgemvModel.ResponseEntity;
//import com.vgemv.jsutilitysdk.model.VgemvModel.StartLiveRequest;
//import com.vgemv.jsutilitysdk.model.VgemvModel.StartLiveResponse;
//import com.vgemv.jsutilitysdk.model.VgemvModel.StreamingLiveRequest;
//
//import java.util.Date;
//import java.util.List;
//
//public class StreamToVgemvService {
//
//    private String usercode = "liujs";
//    private String password = "Js888!@#";
//    private String validcode;
//    private String devicecode = "bf00978e-ac1f-4ac6-b375-28249e4e85ca";
//
//    private String nfj_AdaptAddress = "rtmp://testvliveupload.nfapp.southcn.com:8800/director_push/762_1604401360803?token=DB13BF0CEFF7FEC54B6E52E2B79D14A2E865F270D6FE7CDFE72CED86C92F738DE28B0397";
//    private String nfj_nonAdaptAddress = "rtmp://testvliveupload.nfapp.southcn.com:8800/director_push/762_1604400159210?token=1A08E452831C8807F0A42CCB6CF5E70021683AE4A2984C6F16DE2BD5E3DBF07CC025634B";
//    private String localpush = "rtmp://169.vgemv.com:48800/live/test";
//    private String js_testAddress = "rtmp://169.vgemv.com:48800/director_push/1094_1604458159066?vhost=director&token=562d4ba096794d08-1607050159";
//    public String rtmpAddress = localpush;
//    private String address_kuaihd_test = "http://120.238.244.205/live_cms";
//    private String host = address_kuaihd_test;
//    private Handler mHandler_jsNet;
//    private HandlerThread mht;
//
//
////    private IGajNetworkManager mNetworkManager = mNetworkManager;
////    private IGajNetworkManager mNetworkManager = GajOhNetworkManager.sharedInstance();
//    private JsRequestInterface mNetworkManager = JsRequestManager.sharedInstance();
//
//
//    private Long ml_roomId;
//    private Long ml_liveId;
//    private boolean mb_isLive = false;
//
//
//    static private final int Msg_Login = 1;
//    static private final int Msg_GetRoomList = 2;
//    static private final int Msg_GetRoomInfo = 3;
//    static private final int Msg_StartLive = 4;
//    static private final int Msg_StartStream = 5;
//    static private final int Msg_EndLive = 6;
//
//    public interface IStreamToVgemvServiceCallback {
//       public void onVgemvStartRtmp();
//       public void onVgemvStopRtmp();
//    }
//
//    public IStreamToVgemvServiceCallback mStreamToVgemvServiceCallback;
//
//    public StreamToVgemvService() {
//
//        mht = new HandlerThread("JsNetwork");
//        mht.start();
//        mHandler_jsNet = new Handler(mht.getLooper()) {
//            @Override
//            public void handleMessage(Message msg) {
//                switch (msg.what) {
//                    case Msg_Login: {
//                        login(false);
//                        break;
//                    }
//                    case Msg_GetRoomList: {
//                        getRoomList();
//                        break;
//                    }
//                    case Msg_GetRoomInfo: {
//                        getRoomInfo();
//                        break;
//                    }
//                    case Msg_StartLive: {
//                        startLive();
//                        break;
//                    }
//                    case Msg_StartStream: {
//                        startStream();
//                        break;
//                    }
//                    case Msg_EndLive: {
//                        stopLive();
//                        break;
//                    }
//                }
//            }
//        };
//    }
//
//
//    public void stopStreaming() {
//        //停止直播
//        mb_isLive = false;
//        if(mStreamToVgemvServiceCallback != null) {
//            mStreamToVgemvServiceCallback.onVgemvStopRtmp();
//        }
//        Message msg = Message.obtain();
//        msg.what = Msg_EndLive;
//        mHandler_jsNet.dispatchMessage(msg);
//    }
//    public void startStreaming() {
//                //开启直播
//                mb_isLive = true;
//                Message msg = Message.obtain();
//                msg.what = Msg_Login;
//                mHandler_jsNet.dispatchMessage(msg);
//        }
//
//        private void login(final boolean mb_isGotoNext) {
//            //调用相关接口;
//            LoginRequestEntity lLoginReqeust = new LoginRequestEntity();
//            lLoginReqeust.setAppVersion("148");
//            lLoginReqeust.setDeviceCode(devicecode);
//            lLoginReqeust.setNetType(0);
//            lLoginReqeust.setOs("android 27");
//            lLoginReqeust.setPwdEncrypt(1);
//
//            String ps = "";
//            try {
//                ps = GajMd5Service.md5Encode(password);
//            }
//            catch (Exception e) {
//                e.printStackTrace();
//            }
//            lLoginReqeust.setUserPasswd(ps);
//            lLoginReqeust.setUserCode(usercode);
//            lLoginReqeust.setValidcode("");
//
//            String string1 = String.format("%s/%s",host,"api/login.action");
//
//
//            mNetworkManager.postRequest(string1, lLoginReqeust, new JsRequestCallback() {
//                @Override
//                public void onRequestResponse(Boolean isSuccess, int resultCode, String result, String detailInfo, Exception e) {
////                    GajLogManager.log(detailInfo);
//                    LoginResponse lr = ResponseEntity.fromJsonString(result,LoginResponse.class);
//                    validcode = lr.getValidcode();
//
//                    if(mb_isGotoNext) {
//                        Message msg = Message.obtain();
//                        msg.what = Msg_GetRoomList;
//                        mHandler_jsNet.dispatchMessage(msg);
//                    }
//                    else {
////                        mht.quitSafely();
//                    }
//                }
//            });
//
//        }
//
//        private void getRoomList() {
//
//
//
//            LiveRoomListRequest lrlr = new LiveRoomListRequest();
//            //页码从1开始.
//            lrlr.setPagenum(0);
//            lrlr.setPagesize(2147483647);
//            lrlr.setType(1);
//            lrlr.setUserCode(usercode);
//            lrlr.setValidcode(validcode);
//            String string = String.format("%s/%s",host,"api/liveRoom/listLiveRoom.action");
//            mNetworkManager.postRequest(string, lrlr, new JsRequestCallback() {
//                @Override
//                public void onRequestResponse(Boolean isSuccess, int resultCode, String result, String detailInfo, Exception e) {
//
////                    GajLogManager.log("%s", detailInfo);
//                    LiveRoomListResponse lr = ResponseEntity.fromJsonString(result,LiveRoomListResponse.class);
//                    List<LiveRoomEntity> roomList = lr.getLiveRooms();
//                    LiveRoomEntity room = roomList.get(1);
//                    int status = room.getStatus();
//                    ml_roomId = room.getRoomID();
//
//
//                    if(ml_roomId != null) {
//                        String str = String.format("Room id is %d,name is %s",room.getRoomID(),room.getRoomTitle());
//
//                        Message msg = Message.obtain();
//                        msg.what = Msg_GetRoomInfo;
//                        mHandler_jsNet.dispatchMessage(msg);
//                    }
//                }
//            });
//
//        }
//
//        private void getRoomInfo() {
//            LiveRoomGetRequest llrgr = new LiveRoomGetRequest();
//            llrgr.setRoomID(ml_roomId);
//            llrgr.setUserCode(usercode);
//            llrgr.setValidcode(validcode);
//
//            String string2 = String.format("%s/%s",host,"api/liveRoom/getLiveRoom.action");
//            mNetworkManager.postRequest(string2, llrgr, new JsRequestCallback() {
//                @Override
//                public void onRequestResponse(Boolean isSuccess, int resultCode, String result, String detailInfo, Exception e) {
////                LiveRoomGetResponse response = ResponseEntity.fromJsonString(result,LiveRoomGetResponse.class);
////                    GajLogManager.log("%s", detailInfo);
//                    Message msg = Message.obtain();
//                    msg.what = Msg_StartLive;
//                    mHandler_jsNet.dispatchMessage(msg);
//                }
//            });
//
//        }
//
//
//
//        private void startLive() {
//
//            StartLiveRequest lslr = new StartLiveRequest();
//            Integer audioBitrate = 44100 * 2 * 2 * 8;
//            lslr.setAudioBitrate(audioBitrate);
//            lslr.setAudioChannelCount("2");
//            lslr.setAudioFormat("s16");
//            //这个要设置正确,否则云导播放不正确
//            lslr.setAudioSampleRate("44100");
//            lslr.setRoomID(ml_roomId);
//            lslr.setTryFlag(1);
//            Integer videoBitrate = 1280 * 720 * 4 * 8 * 30;
//            lslr.setVideoBitrate(videoBitrate);
//            lslr.setVideoDimension("1280x720");
//            lslr.setVideoFPS("30");
//            //???
//            lslr.setVideoFormat("17");
//            lslr.setUserCode(usercode);
//            lslr.setValidcode(validcode);
//            lslr.setDeviceCode(devicecode);
//
//            String string3 = String.format("%s/%s",host,"api/liveTask/startLive.action");
//            mNetworkManager.postRequest(string3, lslr, new JsRequestCallback() {
//                @Override
//                public void onRequestResponse(Boolean isSuccess, int resultCode, String result, String detailInfo, Exception e) {
//                    StartLiveResponse response = ResponseEntity.fromJsonString(result,StartLiveResponse.class);
////                    GajLogManager.log("%s", detailInfo);
//                    rtmpAddress = response.getRtmpAddress();
//                    ml_liveId = response.getLiveID();
//                    if(rtmpAddress != null) {
//                        Message msg = Message.obtain();
//                        msg.what = Msg_StartStream;
//                        mHandler_jsNet.dispatchMessage(msg);
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                if(mStreamToVgemvServiceCallback != null) {
//                                    mStreamToVgemvServiceCallback.onVgemvStartRtmp();
//                                }
//                            }
//                        }).start();
//                    }
//                }
//            });
//        }
//
//        private void startStream() {
//            StreamingLiveRequest p = new StreamingLiveRequest();
//            p.setLiveID(ml_liveId);
//            p.media_bitrate = 1644736;
//            p.network_bitrate = 1391879;
//            p.media_encode_avg_bitrate = 14210520;
//            p.ts = new Date().getTime()+"";
//            p.setUserCode(usercode);
//            p.setValidcode(validcode);
//            String string3 = String.format("%s/%s",host,"api/liveTask/streamingLive.action");
//            mNetworkManager.postRequest(string3, p, new JsRequestCallback() {
//                @Override
//                public void onRequestResponse(Boolean isSuccess, int resultCode, String result, String detailInfo, Exception e) {
//
//                    StartLiveResponse response = ResponseEntity.fromJsonString(result, StartLiveResponse.class);
////                    GajLogManager.log("%s", detailInfo);
//                    if(mb_isLive) {
//                        Message msg = Message.obtain();
//                        msg.what = Msg_StartStream;
//                        mHandler_jsNet.sendMessageDelayed(msg, 2000);
//                    }
//                }
//            });
//        }
//
//        private void stopLive() {
//            EndLiveRequest p = new EndLiveRequest();
//            p.setLiveID(ml_liveId);
//            p.setUserCode(usercode);
//            p.setValidcode(validcode);
//            String string3 = String.format("%s/%s",host,"api/liveTask/endLive.action");
//            mNetworkManager.postRequest(string3, p, new JsRequestCallback() {
//                @Override
//                public void onRequestResponse(Boolean isSuccess, int resultCode, String result, String detailInfo, Exception e) {
//                    StartLiveResponse response = ResponseEntity.fromJsonString(result, StartLiveResponse.class);
//                    mHandler_jsNet.getLooper().quit();
//                    mht.quit();
//                    mht = null;
//                }
//            });
//
//        }
//
//
//}
